<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>

 <script type="text/javascript">
 Array.prototype.each = function(fn){
	    fn = fn || Function.K;
	     var a = [];
	     var args = Array.prototype.slice.call(arguments, 1);
	     for(var i = 0; i < this.length; i++){
	         var res = fn.apply(this,[this[i],i].concat(args));
	         if(res != null) a.push(res);
	     }
	     return a;
	};
	
	Array.prototype.contains = function(item){
	    return RegExp(item).test(this);
	};
	Array.prototype.uniquelize = function(){
	     var ra = new Array();
	     for(var i = 0; i < this.length; i ++){
	         if(!ra.contains(this[i])){
	            ra.push(this[i]);
	         }
	     }
	     return ra;
	};
	
	
	Array.complement = function(a, b){
	     return Array.minus(Array.union(a, b),Array.intersect(a, b));
	};
	
	Array.intersect = function(a, b){
	     return a.uniquelize().each(function(o){return b.contains(o) ? o : null});
	};
	
	
	Array.minus = function(a, b){
	     return a.uniquelize().each(function(o){return b.contains(o) ? null : o});
	};
	
	Array.union = function(a, b){
	     return a.concat(b).uniquelize();
	};
 
 </script>
 
 <script>
     var a = [1,2,3,4];
     var b = [3,4,5,6];
     alert(Array.intersect(a,b));
</script>
<body>
    
</body>
</html>